﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using Newtonsoft.Json;
namespace Utilities
{
    public class SerializeHelper
    {

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string path) where T : class
        {
            if (!File.Exists(path))
            {
                return null;
            }
            string strText = File.ReadAllText(path);
            return JsonConvert.DeserializeObject<T>(strText);
        }


        /// <summary>
        /// 序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static void Serialize<T>(T item, string path)
        {
            string strText = JsonConvert.SerializeObject(item, Newtonsoft.Json.Formatting.Indented);
            File.WriteAllText(path, strText);

        }




        ///// <summary>
        ///// 反序列化
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="SerializeText"></param>
        ///// <returns></returns>
        //public static T Deserialize<T>(string SerializeText) where T : class
        //{
        //    if (string.IsNullOrEmpty(SerializeText.Trim()))
        //        return default(T);
        //    try
        //    {
        //        System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
        //        using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(SerializeText)))
        //        {
        //            return serializer.ReadObject(ms) as T;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        return default(T);
        //    }
        //}


        ///// <summary>
        ///// 序列化
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="item"></param>
        ///// <returns></returns>
        //public static string Serialize<T>(T item)
        //{
        //    System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(item.GetType());
        //    using (MemoryStream ms = new MemoryStream())
        //    {
        //        serializer.WriteObject(ms, item);
        //        return Encoding.UTF8.GetString(ms.ToArray());
        //    }
        //}


    }
    internal class TextSerializer
    {
        public static string GetSerializeText(object data)
        {
            string tStr = string.Empty;
            System.Reflection.PropertyInfo[] properties = data.GetType().GetProperties(BindingFlags.Instance
                | BindingFlags.Public);
            if (properties.Length <= 0)
            {
                return tStr;
            }
            foreach (PropertyInfo item in properties)
            {
                string name = item.Name;
                object value = item.GetValue(data, null);
                if (item.Name.StartsWith("_"))
                {
                    tStr += string.Format("{0}:{1}" + Environment.NewLine, name, value);
                }
            }
            tStr = tStr.TrimEnd();
            return tStr;
        }

        public static T GetModelFormText<T>(T t, string SerializeText)
        {
            if (t == null || string.IsNullOrEmpty(SerializeText))
            {
                return default(T);
            }

            string[] strs = SerializeText.Split(new string[1] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (var item in strs)
            {
                string[] s = item.Split(':');
                if (s.Length >= 2)
                {
                    dic.Add(s[0], s[1]);
                }
            }

            System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            if (properties.Length <= 0)
            {
                return default(T);
            }
            foreach (PropertyInfo item in properties)
            {
                string name = item.Name;
                if (dic.ContainsKey(name))
                {
                    string strValue = dic[name];
                    object objValue = null;
                    if (string.IsNullOrEmpty(strValue) == false)
                    {
                        objValue = Convert.ChangeType(strValue, item.PropertyType);
                    }
                    item.SetValue(t, objValue, null);
                }
            }

            return t;
        }

    }

}
